﻿
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO

IF EXISTS (SELECT 1 FROM sys.objects WHERE [object_id] = OBJECT_ID(N'[dbo].[fn_flow_get_jump_target_name]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [dbo].[fn_flow_get_jump_target_name];
GO
CREATE FUNCTION [dbo].[fn_flow_get_jump_target_name]
(
    @FlowId     int,
    @ToFlowId   int,
    @ToStepId   int
)
RETURNS nvarchar(300)
AS
BEGIN

DECLARE @targetName nvarchar(300);

IF ( @ToFlowId = 0 OR @ToFlowId = @FlowId )
BEGIN
    -- jump within the current flow
    IF ( @ToStepId <= 0 )
    BEGIN
        SET @targetName = N'《文档结束》';
    END
    ELSE
    BEGIN
        SELECT @targetName = Step_Name
        FROM  dbo.UDS_Flow_Step (NOLOCK)
        WHERE Flow_ID = @FlowID AND Step_ID = @ToStepID;
    END
END
ELSE
BEGIN
    -- jump to the step in other flows
    SELECT @targetName = (f.Flow_Name + N' - ' + s.Step_Name)
        FROM dbo.UDS_Flow f (NOLOCK) INNER JOIN dbo.UDS_Flow_Step s (NOLOCK) ON f.Flow_ID = s.Flow_ID
        WHERE s.Flow_ID = @ToFlowId AND s.Step_ID = 1; -- always jump to the first step of the flow
END

RETURN @targetName;

END
GO